//
//  HomeViewController.m
//  ProfilePicture
//
//  Created by 张云江 on 2019/10/14.
//  Copyright © 2019年 张云江. All rights reserved.
//

#import "HomeViewController.h"
#import "MyViewController.h"
#import "VipViewController.h"

#import "SDCycleScrollView.h"
#import "OftenViewController.h"
#import "PassportViewController.h"
#import "TestViewController.h"
#import "OtherViewController.h"
#import "DescribeViewController.h"

@interface HomeViewController () <UIScrollViewDelegate,CAAnimationDelegate>
{
    UIButton *vip;
}
@property (nonatomic, strong) UIScrollView   *contentView;
@property (nonatomic, strong) UIScrollView   *topTitleView;
@property (nonatomic, strong) CustomBtn      *replaceBtn;
@property (nonatomic, strong) NSArray        *viewControllers;
@property (nonatomic, strong) NSMutableArray *topBtns;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)setNav
{
    self.customNavBar.hidden = YES;
}

- (void)creatUI
{
    self.topBtns = [NSMutableArray array];
    self.view.backgroundColor = kUIColorFromRGB(0xF3F5FF);
    
    [self setheader];
    [self setChildControllView];
    [self setupTitleScrollView];
    [self setupContentScrollView];
    [self setupAllTitleButtonView];
}

- (void)setheader
{
    UIView *haerderBg = [[UIView alloc]init];
    haerderBg.backgroundColor = [UIColor whiteColor];
    haerderBg.layer.cornerRadius = kRatio(26);
    haerderBg.layer.masksToBounds = YES;
    [self.view addSubview:haerderBg];
    [haerderBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(-kRatio(26));
        make.height.mas_equalTo(kRatio(226));
    }];
    
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
    UILabel *appName = [[UILabel alloc]init];
    appName.text = app_Name;
    appName.font = [UIFont systemFontOfSize:kRatio(24) weight:100];
    [haerderBg addSubview:appName];
    [appName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(haerderBg).offset(kRatio(20));
        make.top.equalTo(haerderBg).offset(kRatio(68));
    }];
    
    UIButton *setting = [[UIButton alloc]init];
    [setting setImage:[UIImage imageNamed:@"home_setting"] forState:UIControlStateNormal];
    [setting addTarget:self action:@selector(onSettingClick) forControlEvents:UIControlEventTouchUpInside];
    [haerderBg addSubview:setting];
    [setting mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(appName);
        make.right.equalTo(haerderBg.mas_right).offset(kRatio(-10));
        make.width.height.mas_equalTo(kRatio(40));
    }];
    
    self.launchAnimation = [LOTAnimationView animationNamed:@"home_vip"];
    self.launchAnimation.cacheEnable = NO;
    self.launchAnimation.contentMode = UIViewContentModeScaleToFill;
    self.launchAnimation.animationSpeed = 1;
    self.launchAnimation.loopAnimation = YES;
    [haerderBg addSubview:self.launchAnimation];
    [self.launchAnimation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(appName);
        make.right.equalTo(setting.mas_left).offset(kRatio(10));
        make.width.height.mas_equalTo(kRatio(66));
    }];
    [self.launchAnimation play];
    
    vip = [[UIButton alloc]init];
    [vip addTarget:self action:@selector(onVipClick) forControlEvents:UIControlEventTouchUpInside];
    [haerderBg addSubview:vip];
    [vip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.launchAnimation);
    }];

    
    NSArray *images = @[[UIImage imageNamed:@"Banner_1"],[UIImage imageNamed:@"Banner_2"]];
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(kRatio(20), kRatio(116), kScreenWidth - kRatio(40), kRatio(92)) imageNamesGroup:images];
    [haerderBg addSubview:cycleScrollView];
    cycleScrollView.layer.cornerRadius = 8;
    cycleScrollView.layer.masksToBounds = YES;
}

- (void)setChildControllView
{
    OftenViewController         *one = [[OftenViewController alloc]init];
    PassportViewController      *two = [[PassportViewController alloc]init];
    TestViewController          *three = [[TestViewController alloc]init];
    OtherViewController         *four = [[OtherViewController alloc]init];
    
    one.onCellClick = ^(HomeInfoMode * _Nonnull info) {
        DescribeViewController *vc = [DescribeViewController initDescribeViewController:info];
        [self.navigationController pushViewController:vc animated:YES];
    };
    two.onCellClick = ^(HomeInfoMode * _Nonnull info) {
        DescribeViewController *vc = [DescribeViewController initDescribeViewController:info];
        [self.navigationController pushViewController:vc animated:YES];
    };
    three.onCellClick = ^(HomeInfoMode * _Nonnull info) {
        DescribeViewController *vc = [DescribeViewController initDescribeViewController:info];
        [self.navigationController pushViewController:vc animated:YES];
    };
    four.onCellClick = ^(HomeInfoMode * _Nonnull info) {
        DescribeViewController *vc = [DescribeViewController initDescribeViewController:info];
        [self.navigationController pushViewController:vc animated:YES];
    };
    self.viewControllers = @[one,two,three,four];
}

#pragma mark - 创建顶部ScrollView
- (void)setupTitleScrollView
{
    UIView *topBar = [[UIView alloc] init];
    [self.view addSubview:topBar];
    [topBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kRatio(200));
        make.left.equalTo(self.view.mas_left);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, kRatio((60))));
    }];
    
    UIScrollView *topScrollView = [[UIScrollView alloc] init];
    topScrollView.pagingEnabled = YES;
    topScrollView.bounces = NO;
    topScrollView.showsHorizontalScrollIndicator = NO;
    [topBar addSubview:topScrollView];
    [topScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.equalTo(topBar);
    }];
    self.topTitleView = topScrollView;
}

- (void)setupAllTitleButtonView
{
    NSArray *titles = @[@"常用尺寸",@"护照签证",@"考试证件照",@"其他证件"];
    
    int count = titles.count;
    NSMutableArray *btnArr = [NSMutableArray array];
    
    for (int i = 0; i < titles.count; i++) {
        CustomBtn *titleBtn = [[CustomBtn alloc]init];
        titleBtn.tag = i;
        [titleBtn addTarget:self action:@selector(onTopBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
        [titleBtn setCustom6withLabel:titles[i]];
        [self.topTitleView addSubview:titleBtn];
        [self.topBtns addObject:titleBtn];
        [btnArr addObject:titleBtn];
    }
    
    [btnArr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:kRatio(60) leadSpacing:kRatio(40) tailSpacing:kRatio(10)];
    
    [btnArr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.topTitleView.mas_centerY);
        make.height.mas_equalTo(kRatio(45));
    }];
    
    self.topTitleView.contentSize = CGSizeMake(kScreenWidth, 0);
    self.contentView.contentSize = CGSizeMake(kScreenWidth * count, 0);
    
    [self onTopBtnEvent:self.topBtns[0]];
}

- (void)setupContentScrollView
{
    UIView *scrollBg = [[UIView alloc] init];
    [self.view addSubview:scrollBg];
    [scrollBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kRatio(260));
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(kScreenHeight - kRatio(260));
    }];
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    [scrollBg addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.equalTo(scrollBg);
    }];
    self.contentView = scrollView;
}

- (void)onTopBtnEvent:(CustomBtn *)button
{
    int index = button.tag;
    
    [self addChildVcViewIntoScrollView:index];
    [self setupTitleCenter:button];
    
    [self.contentView setContentOffset:CGPointMake(kScreenWidth * index, 0) animated:YES];
    [self onBtnClick:button];
}

- (void)onSettingClick
{
    MyViewController *vc = [[MyViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)onVipClick
{
    VipViewController *vc = [[VipViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)onBtnClick:(CustomBtn *)button
{
    int index = button.tag;
    if (button != _replaceBtn) {
        
        [button setCustom6Selected:YES];
        [_replaceBtn setCustom6Selected:NO];
        
        _replaceBtn = button;
        
    } else {
        [_replaceBtn setCustom6Selected:YES];
    }
}

- (void)setupTitleCenter:(UIButton *)btn
{
    if (_topTitleView.contentSize.width > kScreenWidth) {
        CGPoint offsetPoint = _topTitleView.contentOffset;
        offsetPoint.x =  btn.center.x -  kScreenWidth / 2 ;
        if (offsetPoint.x < 0) {
            offsetPoint.x = 0;
        }
        CGFloat maxX = (_topTitleView.contentSize.width - kScreenWidth) > 0 ? (_topTitleView.contentSize.width - kScreenWidth) : kScreenWidth;
        
        if (offsetPoint.x > maxX) {
            offsetPoint.x = maxX;
        }
        [_topTitleView setContentOffset:offsetPoint animated:YES];
    }
}

- (void)addChildVcViewIntoScrollView:(NSInteger)index
{
    UIViewController *vc = self.viewControllers[index];
    
    vc.view.frame = CGRectMake(kScreenWidth * index, 0, kScreenWidth, kScreenHeight - kRatio(260));
    
    [self.contentView addSubview:vc.view];
}

#pragma mark - scrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = (NSInteger) scrollView.contentOffset.x / kScreenWidth;
    
    [self onBtnClick:self.topBtns[index]];
    
    [self addChildVcViewIntoScrollView:index];
    [self setupTitleCenter:self.topBtns[index]];
}
@end
